package com.dream.controller;

import com.alibaba.fastjson.JSON;
import com.dream.pojo.Dormitory;
import com.dream.pojo.Student;
import com.dream.service.DormitoryService;
import com.dream.service.StudentService;
import com.dream.service.impl.DormitoryServiceImpl;
import com.dream.service.impl.StudentServiceImpl;
import com.dream.utils.PageUtil;
import com.dream.utils.ResultCommon;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/DormitoryController.do")
public class DormitoryController extends BaseServlet {
    DormitoryService dormitoryService = new DormitoryServiceImpl();

    StudentService studentService=new StudentServiceImpl();

    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //查询所有宿舍
        List<Dormitory> dormitories = dormitoryService.list();
        //转换为JSON字符串
        String jsonString = JSON.toJSONString(dormitories);
        System.out.println("jsonString=" + jsonString);
        response.getWriter().write(jsonString);
    }

    //查询所有的院系
    public void getAllDormitory(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        ResultCommon resultCommon=null;
        List<Dormitory> dormitories = dormitoryService.list();
        if (dormitories.size() > 0) {
            resultCommon=new ResultCommon(200,"查询成功",dormitories);
        } else {
            resultCommon=new ResultCommon(500,"查询异常",null);
        }
        String toJSONString = JSON.toJSONString(resultCommon);
        response.getWriter().write(toJSONString);
    }

    public void listpage(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String dormitoryName = request.getParameter("dormitoryName"); //条件1
        String pageIndex = request.getParameter("pageIndex"); //条件4  当前页码
        if(pageIndex==null||"".equals(pageIndex)){
            pageIndex="1";//默认值
        }
        String pageSize = request.getParameter("pageSize"); //条件5   页面大小
        if(pageSize==null||"".equals(pageSize)){
            pageSize="5";//默认值
        }
        //条数
        long totalCount = dormitoryService.totalCount(dormitoryName);
        //每页数据
        List<Dormitory> dormitories = dormitoryService.list(dormitoryName, Long.valueOf(pageIndex), Long.valueOf(pageSize));
        //封装一个分页的工具类
        PageUtil<Dormitory> pageUtil=new PageUtil<Dormitory>(Long.valueOf(pageIndex), Long.valueOf(pageSize),totalCount,dormitories);
        //存值转发
        request.setAttribute("pageUtil",pageUtil);
        request.getRequestDispatcher("viewDormitoryInfo.jsp").forward(request,response);
    }

    public void add(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        ResultCommon resultCommon=null;
        String dormitoryId = request.getParameter("dormitoryId");
        String dormitoryName = request.getParameter("dormitoryName");
        String remark = request.getParameter("remark");
        Dormitory dormitory=new Dormitory(dormitoryId,dormitoryName,remark);
        int count = dormitoryService.add(dormitory);
        if (count > 0) {
            resultCommon=new ResultCommon(200,"新增成功",null);
        } else {
            resultCommon=new ResultCommon(500,"新增异常",null);
        }
        String toJSONString = JSON.toJSONString(resultCommon);
        response.getWriter().write(toJSONString);
    }

    public void goupdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String dormitoryId=request.getParameter("dormitoryId");
        request.setAttribute("dormitoryId",dormitoryId);
        Dormitory dormitory = dormitoryService.findById(dormitoryId);
        request.setAttribute("dormitory",dormitory);
        request.getRequestDispatcher("updateDormitory.jsp").forward(request,response);
    }

    public void update(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        ResultCommon resultCommon=null;
        String dormitoryId = request.getParameter("dormitoryId");
        String dormitoryName = request.getParameter("dormitoryName");
        String remark = request.getParameter("remark");
        Dormitory dormitory=new Dormitory(dormitoryId,dormitoryName,remark);
        int count = dormitoryService.update(dormitory);
        if (count > 0) {
            resultCommon=new ResultCommon(200,"更新成功",null);
        } else {
            resultCommon=new ResultCommon(500,"更新异常",null);
        }
        String toJSONString = JSON.toJSONString(resultCommon);
        response.getWriter().write(toJSONString);
    }

    public void delete(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String dormitoryId = request.getParameter("dormitoryId");
        //判断这个宿舍有没有学生
        List<Student> studentList= studentService.findStudentsByDormitoryId(dormitoryId);
        if(studentList.size()>0){
            response.getWriter().write("<script>alert('宿舍在使用中，勿删除！');location.href='/DormitoryController.do?action=listpage';</script>");
        }else{
            int count = dormitoryService.deleteById(dormitoryId);
            if(count>0){
                response.getWriter().write("<script>alert('宿舍删除成功');location.href='/DormitoryController.do?action=listpage';</script>");
            }else{
                response.getWriter().write("<script>alert('宿舍删除异常');location.href='/DormitoryController.do?action=listpage';</script>");
            }
        }
    }
}
